package gao.com.http;

import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpStatus;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

/**
 * Created by admin on 2015/12/25.
 */
public class HttpUrlConnectionUtil {

    public static String execute(Request mRequest) throws IOException {

        switch (mRequest.method) {
            case GET:
                return get(mRequest);
            case POST:
                return post(mRequest);
            case PUT:
                return get(mRequest);
            case DELETE:
                return get(mRequest);
        }
        return null;
    }

    private static String get(Request mRequest) throws IOException {
        HttpURLConnection mConnection = (HttpURLConnection) new URL(mRequest.url).openConnection();
        mConnection.setRequestMethod(mRequest.method.name());
        mConnection.setConnectTimeout(15 * 3000);
        mConnection.setReadTimeout(15 * 3000);

        addHeaders(mConnection, mRequest.headers);

        int status = mConnection.getResponseCode();
        if (status == HttpStatus.SC_OK) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            InputStream is = mConnection.getInputStream();
            byte[] buffer = new byte[2048];
            int len;
            while ((len = is.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            is.close();
            out.flush();
            out.close();
            return new String(out.toByteArray());
        }
        return null;
    }

    private static String post(Request mRequest) throws IOException {
        HttpURLConnection mConnection = (HttpURLConnection) new URL(mRequest.url).openConnection();
        mConnection.setRequestMethod(mRequest.method.name());
        mConnection.setConnectTimeout(15 * 3000);
        mConnection.setReadTimeout(15 * 3000);

        addHeaders(mConnection, mRequest.headers);

        OutputStream os = mConnection.getOutputStream();
        os.write(mRequest.content.getBytes());

        int status = mConnection.getResponseCode();
        if (status == HttpStatus.SC_OK) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            InputStream is = mConnection.getInputStream();
            byte[] buffer = new byte[2048];
            int len;
            while ((len = is.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            is.close();
            out.flush();
            out.close();
            return new String(out.toByteArray());
        }
        return null;
    }


    private static void addHeaders(HttpURLConnection mConnection, HashMap<String, String> headers) {
        if (headers == null || headers.size() == 0) {
            return;
        }
        for (HashMap.Entry<String, String> entry : headers.entrySet()) {
            mConnection.addRequestProperty(entry.getKey(), entry.getValue());
        }
    }
}
